package com.gzhryc.event.dao.db;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;

import com.gzhryc.event.dao.enums.TimeTaskEnum;
import com.gzhryc.jdbc.annotaion.Column;
import com.gzhryc.jdbc.annotaion.Id;
import com.gzhryc.jdbc.annotaion.Table;

/**
 * 定时任务
 * @author 张浩
 * @version 2017-10-12
 */
@Table(name="sys_time_task")
public class TimeTask {

	@Id
	protected Long id;
	@Column
	protected String name;			//任务名称
	@Column(type="text")
	protected String content;		//任务内容，JSON字符串
	//控制
	@Column
	protected Integer status;		//可用状态，1:未启用，2:已启用，3:已加载，4：已完成
	@Column(name="last_run_date")
	protected Date lastRunDate;		//最后执行时间
	//执行策略
	@Column(name="handle_class",length=1024)
	protected String handleClass;	//处理类，带包名
	@Column(name="run_type")
	protected Integer runType;		//执行类型
	@Column(name="quartz_cron")
	protected String quartzCron;	//Cron表达式
	@Column
	protected Integer frequency;	//执行次数
	@Column
	protected Integer year;			//执行年
	@Column
	protected Integer month;		//执行月
	@Column
	protected Integer day;			//执行天
	@Column
	protected Integer hour;			//执行小时
	@Column
	protected Integer minute;		//执行分钟
	@Column
	protected Integer second;		//执行秒
	//辅助属性
	@Column(name="create_date")
	protected Date createDate;		//创建时间
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	public Date getLastRunDate() {
		return lastRunDate;
	}
	public void setLastRunDate(Date lastRunDate) {
		this.lastRunDate = lastRunDate;
	}
	public String getHandleClass() {
		return handleClass;
	}
	public void setHandleClass(String handleClass) {
		this.handleClass = handleClass;
	}
	public Integer getRunType() {
		return runType;
	}
	public void setRunType(Integer runType) {
		this.runType = runType;
	}
	public String getQuartzCron() {
		return quartzCron;
	}
	public void setQuartzCron(String quartzCron) {
		this.quartzCron = quartzCron;
	}
	public Integer getFrequency() {
		return frequency;
	}
	public void setFrequency(Integer frequency) {
		this.frequency = frequency;
	}
	public Integer getYear() {
		return year;
	}
	public void setYear(Integer year) {
		this.year = year;
	}
	public Integer getMonth() {
		return month;
	}
	public void setMonth(Integer month) {
		this.month = month;
	}
	public Integer getDay() {
		return day;
	}
	public void setDay(Integer day) {
		this.day = day;
	}
	public Integer getHour() {
		return hour;
	}
	public void setHour(Integer hour) {
		this.hour = hour;
	}
	public Integer getMinute() {
		return minute;
	}
	public void setMinute(Integer minute) {
		this.minute = minute;
	}
	public Integer getSecond() {
		return second;
	}
	public void setSecond(Integer second) {
		this.second = second;
	}
	public Date getCreateDate() {
		return createDate;
	}
	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}	
	
	public String getRunTypeName(){
		return TimeTaskEnum.RunType.note(this.runType);
	}
	
	public String getStatusName(){
		return TimeTaskEnum.Status.note(this.status);
	}
	
	public String getRunDateStr(){
		if(TimeTaskEnum.RunType.Once.index().equals(this.getRunType())){
			return this.getYear()+"-"+this.getMonth()+"-"+this.getDay()+" "+this.getHour()+":"+this.getMinute()+":"+this.getSecond();
		}else if(TimeTaskEnum.RunType.ManyTimes.index().equals(this.getRunType())){
			return DateFormatUtils.format(new Date(), "yyyy-MM-dd")+" "+this.getHour()+":"+this.getMinute()+":"+this.getSecond();
		}
		return null;
	}
	
	public void setRunDate(Date runDate){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(runDate);
		this.setYear(calendar.get(Calendar.YEAR));
		this.setMonth(calendar.get(Calendar.MONTH) + 1); //从0开始？
		this.setDay(calendar.get(Calendar.DAY_OF_MONTH));
		this.setHour(calendar.get(Calendar.HOUR_OF_DAY));
		this.setMinute(calendar.get(Calendar.MINUTE));
		this.setSecond(calendar.get(Calendar.SECOND));
	}
}
